package com.hyg.lib_music.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_music.R;
import com.hyg.lib_music.modle.CategoryMusicThreeData;
import com.hyg.lib_music.modle.NewMusicCategoryData;
import com.hyg.lib_music.modle.RecommedMusicData;
import com.hyg.lib_music.ui.activity.CategoryMusicListActivity;
import com.hyg.lib_music.ui.activity.MusicMainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Administrator
 * @date: 2021/4/8
 */
public class MusicCategorySecondAdapter extends RecyclerView.Adapter<MusicCategorySecondAdapter.ViewHolder> {
    ArrayList<CategoryMusicThreeData.DataDTO> list;
    Context context;
    String imageUrl;

    public MusicCategorySecondAdapter(Context context, ArrayList<CategoryMusicThreeData.DataDTO> list, String imageUrl) {
        this.list = list;
        this.context = context;
        this.imageUrl = imageUrl;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_categoty_second_rl, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).logoUrl).into(holder.itemIconIV);
        holder.titleTv.setText(list.get(position).cateName);
        holder.controlLn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("lgb", "onClick: play");
                ArrayList<Music> music_list = new ArrayList<>();
                for(CategoryMusicThreeData.DataDTO.MusicListDTO data :list.get(position).musicList){
                        music_list.add( new Music(data.id, data.musicName,data.musician, data.musicDuration,
                                data.region, data.musicUrl, (data.favorite==1),
                                0,0, "",
                                false,
                                true,false));
                    }
                MusicMainActivity.serviceBinder.addPlayList(music_list,music_list.get(0));
            }
        });

        holder.rootRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("lgb", "onClick: root");
                Intent intent=new Intent(context, CategoryMusicListActivity.class);
                intent.putExtra("imageUrl", imageUrl);
                intent.putExtra("data",list.get(position));
//                intent.putExtra("id",list.get(position).id);
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView playIconIV, itemIconIV;
        TextView titleTv;
        LinearLayout controlLn;
        RelativeLayout rootRl;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemIconIV = itemView.findViewById(R.id.iv_icon);
            titleTv = itemView.findViewById(R.id.tv_title);
            controlLn = itemView.findViewById(R.id.ln_play_control);
            playIconIV = itemView.findViewById(R.id.iv_control_play_btn);
            rootRl = itemView.findViewById(R.id.rl_root);
        }
    }


}
